package com.zr.planclass.controller;

import com.zr.meiju.StatusEnum;
import com.zr.planclass.pojo.PlanClassAddVo;
import com.zr.planclass.pojo.PlanClassSelectVo;
import com.zr.planclass.pojo.PlanClassStatusVo;
import com.zr.planclass.pojo.PlanClassUpdateVo;
import com.zr.planclass.service.PlanClassService;
import com.zr.plant.pojo.XiaLaVo;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 86151 on 2019/6/14.
 */
@RestController
@CrossOrigin
public class PlanClassController {
    @Autowired
    private PlanClassService planClassService;

    /**
     * 添加
     * @param planClassAddVo
     * @param bindingResult
     * @return
     */
    @PostMapping("planCategory/add")
    public ResultVo addPlanClass(@RequestBody @Valid PlanClassAddVo planClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        List<PlanClassAddVo> planClassAddVoList = new ArrayList<>();
        planClassAddVoList.add(planClassAddVo);
        return planClassService.addPlanClass(planClassAddVoList);
    }

    /**
     * 修改接口
     * @param planClassUpdateVo
     * @param bindingResult
     * @return
     */
    @PostMapping("planCategory/update")
    public ResultVo updatePlanClass(@RequestBody @Valid PlanClassUpdateVo planClassUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return planClassService.updatePlanClass(planClassUpdateVo);
    }

    /**
     * 修改状态接口
     * @param planClassStatusVo
     * @param bindingResult
     * @return
     */
    @PostMapping("planCategory/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid PlanClassStatusVo planClassStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage(),"500");
        }
        return planClassService.updateStatus(planClassStatusVo);
    }

    /**
     * 查看
     */
    @GetMapping("/queryPlanClassById")
    public ResultVo queryPlanClassById(@RequestParam("id")Integer id){

        return planClassService.queryPlanClassById(id);
    }
    /**
     * 分页查询
     */
    @PostMapping("planCategory/queryPage")
    public ResultVo<AllRecords> queryPage(@RequestBody PlanClassSelectVo planClassSelectVo){

        return planClassService.queryPage(planClassSelectVo);
    }

    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("quotation/InquiryIntroduction")
    public ResultVo importPlanClass(MultipartFile file)throws Exception{
        return planClassService.importPlanClass(file);
    }

    /**
     * 启用禁用下拉框
     * @return
     */
    @PostMapping("material/exportMater")
    @CrossOrigin
    public ResultVo exportMater(){
        return ResultBuildVo.success(null);
    }

    /**
     * 导出
     * @param resp
     * @param code
     * @param name
     * @param status
     * @return
     * @throws Exception
     */
    @RequestMapping("/exportExcel/master")
    public ResultVo exportExcel(HttpServletResponse resp, String code, String name, Boolean status) throws Exception{
        PlanClassSelectVo planClassSelectVo = new PlanClassSelectVo();
        planClassSelectVo.setCode(code);
        planClassSelectVo.setName(name);
        planClassSelectVo.setStatus(status);
        return planClassService.exportExcel(resp,planClassSelectVo);
    }


}
